﻿package com.net.http;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


public class Response {
    private static final String TAG	 = "Response";
	private HttpResponse mResponse = null;
    private boolean mStreamConsumed = false;
    
    public Response(HttpResponse res) {
        mResponse = res;        
    }
    
    public HttpResponse getHttpResponse(){
    	return mResponse;
    }

    public InputStream asStream() throws ResponseException {
    	
    	if(mResponse==null) return null;
        HttpEntity entity = mResponse.getEntity();
        if(entity==null)	return null;
        InputStream is = null;
        try {
            is = entity.getContent();
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null && ceheader.getValue().equalsIgnoreCase("gzip")) {
                is = new GZIPInputStream(is);
            }
        } catch (IllegalStateException e) {
            throw new ResponseException(e.getMessage(), e);
        } catch (IOException e) {
            throw new ResponseException(e.getMessage(), e);
        }
        mResponse = null;
        return is;
    }

    public String asString() throws ResponseException {
        return asString("GBK");
    }
    
    public String asString(String encode) throws ResponseException {
        
        String str = null;
        InputStream is = asStream();
        if (null == is) {
            return str;
        }

        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(is, encode));
            String line = "";
            String NL = System.getProperty("line.separator");
            StringBuffer buf = new StringBuffer();
            while ((line = br.readLine()) != null){
            	buf.append(line + NL);
            }
            str = buf.toString();
        } catch (NullPointerException npe) {
            throw new ResponseException(npe.getMessage(), npe);
        } catch (IOException ioe) {
            throw new ResponseException(ioe.getMessage(), ioe);
        } finally {
            if (br != null) {
                try {
                    setStreamConsumed(true);
                    is.close();
                    br.close();
                } catch (IOException e) {
                    throw new ResponseException(e.getMessage(), e);
                }
            }
        }
        return str;
    }
    
    public Bitmap asBitmap() throws IOException {
    	if (mResponse.getStatusLine().getStatusCode() != 200) {
			throw new IOException("Non OK response: "+ mResponse.getStatusLine().getStatusCode());
		}
		HttpEntity entity = mResponse.getEntity();
		BufferedInputStream bis = new BufferedInputStream(entity.getContent());
		Bitmap bitmap = BitmapFactory.decodeStream(bis);
		bis.close();
		return bitmap;
    }

    public JSONObject asJSONObject() throws ResponseException {
    	JSONObject json = null;
    	String str = asString();
    	if(str==null)return null;
        try {
        	Log.i(TAG,"str;"+str);
            json = new JSONObject(str);
            return json;
        } catch (JSONException jsone) {
            throw new ResponseException(jsone.getMessage() + ":"+ asString(), jsone);
        }
        catch(RuntimeException e){
        	 throw new ResponseException(e.getMessage() + ":"
                     + asString(), e);
        }
    }

    public JSONArray asJSONArray() throws ResponseException {
    	String str = asString();
    	if(str==null||"".equals(str)||"null".equals(str))return null;
        try {
            return new JSONArray(str);
        } 
        catch(RuntimeException e){
          	 throw new ResponseException(e.getMessage() + ":"
                       + asString(), e);
        }
        catch (Exception jsone) {
            throw new ResponseException(jsone.getMessage() + ":"
                    + asString(), jsone);
        }
    }

    private void setStreamConsumed(boolean mStreamConsumed) {
        this.mStreamConsumed = mStreamConsumed;
    }

    public boolean isStreamConsumed() {
        return mStreamConsumed;
    }

}
